package cn.amarone.wineblog.common.appointment;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import java.io.Serializable;
import java.util.HashMap;

/**
 * 统一响应参数封装类
 *
 * @author amarone
 * @date 2020/10/09
 */
public class Result extends HashMap<String, Object> implements Serializable {

  private static final String CODE_KEY = "code";
  private static final String MSG_KEY = "msg";
  private static final String DATA_KEY = "data";
  private static final int INITIALCAPACITY = 3;

  public enum Type {
    /** 成功 */
    SUCCESS(0),
    /** 警告 */
    WARN(301),
    /** 认证失败 */
    UNAUTHORIZED(401),
    /** 错误 */
    ERROR(500);

    private final int value;

    Type(int value) {
      this.value = value;
    }

    public int value() {
      return this.value;
    }
  }

  public Result() {}

  public Result(Type type, String msg) {
    super.put(CODE_KEY, type.value);
    super.put(MSG_KEY, msg);
  }

  public Result(Type type, String msg, Object data) {
    super.put(CODE_KEY, type.value);
    super.put(MSG_KEY, msg);
    if (ObjectUtil.isNotNull(data)) {
      super.put(DATA_KEY, data);
    }
  }

  public static Result success(String msg) {
    return new Result(Type.SUCCESS, msg);
  }

  public static Result success() {
    return new Result(Type.SUCCESS, "操作成功!");
  }

  public static Result success(String msg, Object data) {
    return new Result(Type.SUCCESS, StrUtil.isNotEmpty(msg) ? msg : "操作成功!", data);
  }

  public static Result success(Object data) {
    return new Result(Type.SUCCESS, "操作成功!", data);
  }

  public static Result error() {
    return new Result(Type.ERROR, "操作成功!");
  }

  public static Result error(String msg) {
    return new Result(Type.ERROR, msg);
  }

  public static Result warn() {
    return new Result(Type.WARN, "操作成功!");
  }

  public static Result warn(String msg) {
    return new Result(Type.WARN, msg);
  }
}
